#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
import re,sys,os
os.chdir(os.path.split(os.path.realpath(__file__))[0])
sys.path.append(os.path.abspath(".."))

# https://www.cnblogs.com/Liqiongyu/p/5916710.html
# https://github.com/w392807287/Design_pattern_of_python
rename = {
    "1.创建-工厂方法模式-FactoryMethod.py": "Factory_Method.py",
    "2.创建-抽象工厂模式-AbstractFactory.py": "Abstract_Factory.py",
    "3.创建-创建者模式-Builder.py": "Builder.py",
    "4.创建-原型模式-Prototype.py": "Prototype.py",
    "5.创建-单例模式-Singleton.py": "Singleton.py",
    "6.结构-适配器模式-Adapter.py": "Adapter.py",
    "7.结构-代理模式-Proxy.py": "Proxy.py",
    "8.结构-装饰模式-Decorator.py": "Decorator.py",
    "9.结构-桥模式-Bridge.py": "Bridge.py",
    "10.结构-组合模式-Composite.py": "Composite.py",
    "11.结构-外观模式-Facade.py": "facade.py",
    "12.结构-享元模式-Flyweight.py": "Flyweight.py",
    "13.行为-观察者模式-Observer.py": "Observer.py",
    "14.行为-状态模式-State.py": "State.py",
    "15.行为-策略模式-Strategy.py": "Strategy.py",
    "16.行为-职责链模式-ResponsibilityChain.py": "Chain.py",
    "17.行为-命令模式-Command.py": "Command.py",
    "18.行为-访问者模式-Visitor.py": "Visitor.py",
    "19.行为-调停者模式-Mediator.py": "Mediator.py",
    "20.行为-备忘录模式-Memento.py": "Memento.py",
    "21.行为-迭代器模式-Iterator.py": "Iterator.py",
    "22.行为-解释器模式-Interpreter.py": "Interpreter.py",
    "23.行为-模板方法-TemplateMethod.py": "Template_Method.py",
}
files = os.listdir("./")
print(files)
for k, v in rename.items():
    if v in files:
        print(v, "->", k)
        os.rename(v, k)

s = """<details><summary>{names}</summary><pre><code> 
  
```python
{pythons}
```
</code></pre></details>

"""

wfile = {}
# for wfilename in ["创建","结构","行为"]:
#     wfile[wfilename]=open("设计模式-"+wfilename+"类.md","w",encoding="utf-8")
wfile["all"] = open("设计模式.md", "w", encoding="utf-8")

for k, _ in rename.items():
    print(k)
    wfilename, namef = re.findall(
        r"\.(.+?)-", k)[0], "-".join(k.split(".")[:-1])
    print(wfilename, namef)
    with open(k, 'r') as f:
        sb = s.format(pythons=f.read(), names=namef)
        # wfile[wfilename].write(sb)
        # wfile[wfilename].flush()
        wfile["all"].write(sb)
        wfile["all"].flush()

# for wfilename in ["创建","结构","行为"]:
#     wfile[wfilename].close()
wfile["all"].close()
